package com.hightrast.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hightrast.service.api.domain.qo.rastRole.AddUserRoleQo;
import com.hightrast.service.api.entity.RastUserRole;
import com.hightrast.service.mapper.RastUserRoleMapper;
import com.hightrast.service.service.IRastUserRoleService;
import com.hightrast.service.util.BaseEntityUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户角色关联表 服务实现类
 * </p>
 *
 * @author hx
 * @since 2025-01-11
 */
@Service
public class RastUserRoleServiceImpl extends ServiceImpl<RastUserRoleMapper, RastUserRole> implements IRastUserRoleService {


    /**
     * 获取单个用户的角色
     * @param userId
     */
    @Override
    public List<RastUserRole> queryUserRoleByUserId(Long userId) {
        return this.getBaseMapper().selectUserRoleByUserId(userId);
    }

    /**
     * 新增、修改用户角色，每次先删再曾
     */
    @Transactional
    @Override
    public void saveUpdateUserRole(AddUserRoleQo qo) {
        // 每次先删除当前用户绑定的角色再新增
        remove(new LambdaQueryWrapper<RastUserRole>()
                .eq(RastUserRole::getUserId, qo.getUserId()));
        RastUserRole userRole = BeanUtil.copyProperties(qo,RastUserRole.class);
        BaseEntityUtil.save(userRole);
        save(userRole);
    }
}
